<?php

	//Author: Piyush Garg
	//Student ID: S3218012
	//Assignment 3 Task2: Document Markuip Language

	//Loading the TVGuide.xml document

	$dom = new DOMDocument;	
	$dom->load('TVGuide.xml');
	//Get the Channel Element inside the document
	$ChannelElement=$dom->getElementsByTagName("Channel");
	
	//Getting the search value from the command line.
	//Search Term.....
	$search =$argv[1];
	
	$counter=0;
	$condition=false;
	
	foreach($ChannelElement as $ChannelList)
	{
		//Getting the Program Element 
		
		$programElement=$ChannelList->getElementsByTagName("Program");
		
		//Getting the Channel Name
		$ChannelName=$ChannelList->getElementsByTagName("Name");
		$ChannelNameValue=$ChannelName->item(0)->nodeValue;
		
		$counter=0;
		
		//Getting through each Program Element
		foreach($programElement as $programs)
		{
			//Getting the Series element from each program Element
			$SeriesList=$programs->getElementsByTagName("Series");
			$SeriesName=$SeriesList->item(0)->nodeValue;
			
			//Getting the Start Time Element from the Program Element
			$StartList=$programs->getElementsByTagName("Start");
			$StartTime=$StartList->item(0)->nodeValue;
			
			if(strcmp($SeriesName,$search)==0)
			{
				$condition=true;
				
				//Printing the channel name only once,
				//If counter is 0 then print the channel name otherwise dont print again
				if($counter==0)
				{
					echo "$ChannelNameValue";
					echo "\n";
				}
				//Print the Start Time
				echo "Start : ".$StartTime;
				echo "\n";
				$counter++;
			}
		}
		
	}
	
	//If the Seach keyword not found
	if($condition==false)
	{
		//Print Error Message
		echo "Series ".$search." not found.";
		echo "\n";
	}
